<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateDevicesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('devices', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('user_id')->comment('所属代理');
            $table->string('device_sn')->comment('设备编号');
            $table->string('name')->comment('设备名称');
            $table->string('province_code', 10)->nullable()->comment('省');
            $table->string('city_code', 10)->nullable()->comment('市');
            $table->string('district_code', 10)->nullable()->comment('区');
            $table->string('address')->nullable()->comment('详细地址');
            $table->geometry('coordinate')->comment('位置坐标');
            $table->string('status')->default('offline')->comment('设备状态 online：在线；offline：离线；fault：故障');
            $table->json('ports')->comment('充电设备端口状态 0 关闭 1开启');
            $table->json('ladder_price')->comment('设备定价策略 {before_price: xxx, after_price: xxx, boundary_time: xxx}');
            $table->string('remark')->nullable()->comment('设备备注');

            $table->softDeletes();
            $table->timestamps();

            $table->unique('device_sn');
            $table->index('user_id');
            $table->index('province_code');
            $table->index('city_code');
            $table->index('district_code');
            $table->index('status');
            $table->foreign('user_id')->references('id')->on('users');

            $table->spatialIndex('coordinate');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('devices');
    }
}
